package com.accp.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.accp.biz.UsersBiz;
import com.accp.pojo.Users;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
/**
 * 用户业务控制器  usersAction负责调用模型业务类来实现用户的各种业务请求。
 * 接收客户端上传的参数方式：实现ModelDriven接口
 * @author Administrator
 */
public class UsersAction implements ModelDriven<Users>{	
	private Users user = new Users();//接收参数
	@Override
	public Users getModel() {
		return user;
	}
	
	private UsersBiz usersBiz;
	public void setUsersBiz(UsersBiz usersBiz) {
		this.usersBiz = usersBiz;
	}
	/**
	 * 用户登录
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String login(){
		Users users = usersBiz.login(user);
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		if(users!=null){
			Map<String,Object> session = ActionContext.getContext().getSession();//获取seesion作用域对象。
			session.put("user", users);
			return "login";			
		}
		request.put("message", "用户名或密码错误");
		return "relogin";
	}
	/**
	 * 退出登录
	 * @return
	 */
	public String out(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		session.remove("user");
		return "out";
	}
	/**
	 * 修改用户信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String update(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		Users users = (Users) session.get("user");
		users.setNickName(user.getNickName());
		users.setAge(user.getAge());
		users.setSex(user.getSex());
		users.setTelephone(user.getTelephone());
		users.setAddress(user.getAddress());
		boolean flag = usersBiz.update(users);
		if(flag){
			request.put("message", "修改成功");
			return "update";
		}
		return "error";
	}
	/**
	 * 修改用户密码
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String updatePwd(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		Users users = (Users) session.get("user");
		users.setUserPwd(user.getUserPwd());
		boolean flag = usersBiz.update(users);
		if(flag){
			request.put("message", "密码修改成功");
			return "updatePwd";
		}
		return "error";
	}
	/**
	 * 查询所有用户信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String findAll(){
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		Map<String,Object> session =  ActionContext.getContext().getSession();
		Users user = (Users) session.get("user");
		String userName = user.getUserName();
		List<Users> list = usersBiz.findExceptMe(userName);
		if(null!=list){
			request.put("userList", list);
			return "findAll";
		}
		return "error";
	}
	/**
	 * 添加用户
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String add(){
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		boolean flag = usersBiz.add(user);
		if(flag){
			request.put("message", "添加成功");
			return "add";
		}
		return "error";
	}
	/**
	 * 删除用户
	 * @return
	 */
	public String delete(){
	
		HttpServletRequest request = ServletActionContext.getRequest();
		String userName = request.getParameter("userName");
		Users users = usersBiz.findByUserName(userName);
		if(usersBiz.deleteUser(users)){
			return "delete";
		}else{
			return "error";
		}
	}
	/**
	 * 分页查看用户信息
	 * 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String selectFirstPage(){
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		try{
			Map<String,Object> session =  ActionContext.getContext().getSession();
			Users user = (Users) session.get("user");
			String userName = user.getUserName();
			List list = usersBiz.selectByPage("Users", 1, 3,userName);
			int totalPage = usersBiz.searchRecordsNextPage(3, "Users",userName);
			request.put("list", list);
			request.put("totalPage", totalPage);
			request.put("currentPage", 1);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "selectFirstPage";
	}
	/**
	 * 分页查找下一页
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public String selectNextPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int next = Integer.parseInt(request.getParameter("page"));
		try{
			Map<String,Object> session =  ActionContext.getContext().getSession();
			Users user = (Users) session.get("user");
			String userName = user.getUserName();
			List list = usersBiz.selectByPage("Users", next, 3,userName);
			int total = usersBiz.searchRecordsNextPage(3, "Users",userName);
			request.setAttribute("list", list);
			request.setAttribute("currentPage", next);
			request.setAttribute("totalPage", total);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "selectNextPage";
	}
}
